﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using SocialGame.Business;
using Social.Game.DAL;
using SocialGame.Models;
using Omu.ValueInjecter;
using Ext.Net.MVC;
namespace SocialGame.Areas.Games.Controllers
{
    public class QuestionsController : Controller
    {
        //
        // GET: /Games/Question/

        public ActionResult Index()
        {

            return View();
        }


        //
        // GET: /Games/Question/

        public ActionResult Create()
        {
            QuestionModel quest = new QuestionModel();

            return new Ext.Net.MVC.PartialViewResult()
            {
                Model = quest
            };
        }

        //
        // GET: /Games/Question/
        public ActionResult CreateAnswer(String QuestionID)
        {
            AnswerModel answer = new AnswerModel();
            answer.QuestionId = int.Parse(QuestionID);
            return new Ext.Net.MVC.PartialViewResult()
            {
                Model = answer
            };
        }

        //
        // GET: /Games/Question/
        [HttpPost, ValidateInput(false)]
        public ActionResult CreateAnswer(AnswerModel answer)
        {
            Answer aw = new Answer();
            aw.InjectFrom(answer);
            aw.AnswerType = answer.Type.ID;
            aw.AnswerValue = answer.AnswerValue ? 1 : 0;
            aw.QuestionId = answer.QuestionId;
            aw.Point = answer.Point;
            if (aw.ID == 0)
                DbUtil.Database.Insert(aw);
            else
                DbUtil.Database.Update(aw);
            return this.Direct();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(QuestionModel quest)
        {
            Question q = new Question();
            q.InjectFrom(quest);
            q.QuestType = quest.QuestType.QuestionType;
            q.State = quest.State ? 1 : 0;
            q.FieldId = quest.FieldId;
            if (q.ID > 0)
            {
                DbUtil.Database.Update(q);
            }
            else
            {
                DbUtil.Database.Insert(q);
            }
            return this.Direct();
        }

        public ActionResult GetAnwser(StoreRequestParameters parameters, int? QuestionID)
        {
            var db = DbUtil.Database;
            var rs = db.Page<Answer>(parameters.Page, parameters.Limit
                , @"SELECT Answers.*  FROM Answers Where QuestionId = @0", QuestionID);
            Paging<Answer> page = new Paging<Answer>(rs.Items.ToArray(), (int)rs.TotalItems);
            return this.Store(page);
        }

        public ActionResult GetQuestions(StoreRequestParameters parameters)
        {
            var db = DbUtil.Database;
            var rs = db.Page<Question>(parameters.Page, parameters.Limit
                , @"SELECT Questions.*  FROM Questions 
                    JOIN QuestionType ON QuestionType.QuestionType = Questions.QuestType
                    JOIN Fields ON Fields.ID = Questions.FieldID");
            List<QuestionModel> list = new List<QuestionModel>();
            foreach (var r in rs.Items)
            {
                QuestionModel q = new QuestionModel();
                var qt = db.SingleOrDefault<QuestionType>("Where [QuestionType] =@0", r.QuestType);
                var fieldType = db.SingleOrDefault<Social.Game.DAL.Field>("Where [ID] = @0", r.FieldId);
                q.QuestType = new QuestionTypeModel()
                {
                    QuestionName = qt.QuestionName,
                    QuestionType = qt._QuestionType
                };
                q.Field = new FieldsModel()
                {
                    Id = fieldType.ID,
                    FieldName = fieldType.FieldName
                };
                q.InjectFrom(r);
                q.State = r.State == 1;
                list.Add(q);
            }
            Paging<QuestionModel> page = new Paging<QuestionModel>(list, (int)rs.TotalItems);
            return this.Store(page);
        }

        public ActionResult EditAnswerCommand(string ID, string command)
        {
            var db = DbUtil.Database;
            if ("Delete".Equals(command))
            {
                db.Delete("Answers", "ID", null, ID);
                return this.Direct();
            }
            else
            {
                var answers = db.SingleOrDefault<Answer>("WHERE ID = @0", ID);
                AnswerModel model = new AnswerModel();
                model.InjectFrom(answers);
                model.Point = answers.Point.HasValue ? answers.Point.Value : 0;
                model.AnswerValue = answers.AnswerValue.HasValue ? answers.AnswerValue.Value == 1 : false;
                model.Type = new AnswerType(){                    
                    ID = answers.AnswerType.HasValue ? answers.AnswerType.Value : 0
                };
                model.QuestionId = answers.QuestionId.Value;
                return new Ext.Net.MVC.PartialViewResult
                {
                    ViewName = "CreateAnswer",
                    Model = model,
                };
            }
        }

        public ActionResult EditCommand(string QuestionID, string command)
        {
            var db = DbUtil.Database;
            if ("Delete".Equals(command))
            {
                db.Delete("Questions", "ID", null, QuestionID);
                return this.Direct();
            }
            else
            {
                var user = db.SingleOrDefault<Question>("WHERE ID = @0", QuestionID);
                QuestionModel q = new QuestionModel();
                q.InjectFrom(user);
                q.QuestType = new QuestionTypeModel()
                {
                    QuestionType = user.QuestType
                };
                q.Field = new FieldsModel()
                {
                    Id = user.FieldId
                };
                q.State = user.State == 1;
                return new Ext.Net.MVC.PartialViewResult
                {
                    ViewName = "Create",
                    Model = q,
                };
            }
        }
    }
}
